package cn.com.idmy.base.model;

import cn.com.idmy.base.Err;
import cn.com.idmy.base.config.DefaultConfig;
import com.alibaba.fastjson2.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(fluent = true, chain = true)
public class Out implements Serializable {
    @Nullable
    private String msg;
    private int code;
    @Nullable
    private Object data;
    private String appKey = System.getProperty("app.key");

    @JSONField(serialize = false)
    public boolean isOk() {
        return code == 0;
    }

    @JSONField(serialize = false)
    public boolean isErr() {
        return !isOk();
    }

    @NotNull
    public static Out ok(@Nullable Object data) {
        return new Out().data(data);
    }

    @NotNull
    public static Out ok() {
        return new Out().data(null);
    }

    @NotNull
    public static Out err() {
        return new Out().code(DefaultConfig.defaultErrCode);
    }

    @NotNull
    public static Out err(@NotNull Err err) {
        return new Out().code(err.code()).msg(err.msg());
    }
}